#include "file.h"

int FileLineCount(FILE *fp)
/* assume the file end with a new line */
{
    if (!fp) return -1;

    char ch;
    int lcnt = 0;

    /* move fp to file head */
    rewind(fp);

    while(!feof(fp)){
        ch = fgetc(fp);
        if(ch == '\n')
            lcnt ++;
    }

    rewind(fp);

    return lcnt;
}


boolean FileGetLine(FILE *fp, char *buf)
/* assume buf has enough length
   and file format is unix */
{
    int idx = 0;
    char ch;

    while(!feof(fp)){
        ch = fgetc(fp);
        buf[idx] = ch;
        ++ idx;
        if (ch == '\n')
            break;
    }

    buf[idx] = '\0';

    if (!idx)
        return false;

    return true;
}
